package com.paireach.bw.utils;

import com.google.gson.reflect.TypeToken;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.bean.LoginInfo;
import com.paireach.bw.bean.OrdersBean;
import com.paireach.bw.event.AppEvent;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by ${cqc} on 2017/2/3.
 */

public class JsonParse {

    private static final String TAG = "JsonParse";

    public static void parseLoginUser(String result) {
        AppEvent event = new AppEvent();
        event.setTag("parseLoginUser");
        LoginInfo info = GsonUtil.jsonToBean(result, LoginInfo.class);
        if (info == null) {
            event.setCode(2);
            event.setExtraInfo("解析数据失败");
        } else {
            if ("0".equals(info.getLoginResult())) {
                event.setCode(0);
                event.setExtraInfo(info.getRemark());
            } else if ("1".equals(info.getLoginResult())) {
                event.setCode(1);
                event.setObj1(info.getCpDriver());
                event.setExtraInfo(info.getRemark());
            } else {
                event.setExtraInfo("其他情况，请检查数据");
            }
        }
        EventBus.getDefault().post(event);
    }


//    public static void parseCanAcceptOrderList(String json) {
//        AppEvent event = new AppEvent();
//        event.setTag("can_accept_order_list");
//        OnTheRoadBean onTheRoadBean = GsonUtil.jsonToBean(json, OnTheRoadBean.class);
//        if (onTheRoadBean == null) {
//            event.setCode(0);
//            event.setExtraInfo("无运单");
//        } else {
//            event.setCode(1);
//            event.setObj1(onTheRoadBean.getOrders());
//        }
//        EventBus.getDefault().post(event);
//    }


    public static void parseCanAcceptOrderList(String json) {
        AppEvent event = new AppEvent();
        event.setTag("can_accept_order_list");
        try {
            JSONObject object = new JSONObject(json);
            String loadResult = object.getString("loadResult");
            if ("0".equals(loadResult)) {
                event.setCode(0);
                event.setExtraInfo(object.getString("msg"));
            } else if("1".equals(loadResult)){
                event.setCode(1);
                JSONArray array = object.getJSONArray("orders");
                List<DriveOrder> list = (List<DriveOrder>) GsonUtil.jsonToList(array.toString(), new TypeToken<List<DriveOrder>>() {
                }.getType());
                event.setObj1(list);
//                LogUtil.d(TAG,"1--"+list.size()+"--"+list.toString()+list.get(0).getORDER_CODE());
            }else{
                event.setCode(3);
                event.setExtraInfo("只能接一单");
            }
        } catch (JSONException e) {
            e.printStackTrace();
            event.setExtraInfo("解析数据错误");
        }
        EventBus.getDefault().post(event);
    }

    /**
     * 解析配送中列表数据
     */
    public static void parseOnTheRoadOrderList(String json) {
        AppEvent event = new AppEvent();
        event.setTag("on_the_road_order_list");
        try {
            JSONObject object = new JSONObject(json);
            String loadResult = object.getString("loadResult");
            if ("0".equals(loadResult)) {
                event.setCode(0);
                event.setExtraInfo(object.getString("msg"));
            } else {
                event.setCode(1);
                JSONArray array = object.getJSONArray("orders");
                List<DriveOrder> list = (List<DriveOrder>) GsonUtil.jsonToList(array.toString(), new TypeToken<List<DriveOrder>>() {
                }.getType());
                event.setObj1(list);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            event.setExtraInfo("解析数据错误");
        }
        EventBus.getDefault().post(event);
    }

    /**
     * 已完成列表
     */
    public static void parseCompleteOrderList(String json) {
        AppEvent event = new AppEvent();
        event.setTag("complete_order_list");
        try {
            JSONObject object = new JSONObject(json);
            String loadResult = object.getString("loadResult");
            if ("0".equals(loadResult)) {
                event.setCode(0);
                event.setExtraInfo(object.getString("msg"));
            } else {
                event.setCode(1);
                JSONArray array = object.getJSONArray("orders");
                List<DriveOrder> list = (List<DriveOrder>) GsonUtil.jsonToList(array.toString(), new TypeToken<List<DriveOrder>>() {
                }.getType());
                event.setObj1(list);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            event.setExtraInfo("解析数据错误");
        }
        EventBus.getDefault().post(event);
    }


    public static void parseGetOrderCodeDetail(String json) {
        AppEvent event = new AppEvent();
        event.setTag("get_order_code_detail");
        try {
            JSONObject object = new JSONObject(json);
            String loadResult = object.getString("status");
            if ("0".equals(loadResult)) {
                event.setCode(0);
                event.setExtraInfo(object.getString("msg"));
                LogUtil.d(TAG, "0");
            } else {
                event.setCode(1);
                JSONArray array = object.getJSONArray("orders");
                List<OrdersBean> list = (List<OrdersBean>) GsonUtil.jsonToList(array.toString(), new TypeToken<List<OrdersBean>>() {
                }.getType());
                event.setObj1(list);
                LogUtil.d(TAG, "1--" + list.size() + "--" + list.toString() + list.get(0).getORDER_CODE());
            }
        } catch (JSONException e) {
            e.printStackTrace();
            event.setExtraInfo("解析数据错误");
            LogUtil.d(TAG, "解析数据错误");
        }
        EventBus.getDefault().post(event);
    }


}
